package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.dto.IntegrationCouponDetailRespDto;
import com.rockcent.mall.lib.dto.IntegrationCouponRespDto;
import com.rockcent.mall.lib.dto.MallCashCouponDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by zhangjh on 2016/12/19.
 */
@Repository
public class MallCashCouponRepositoryImpl implements MallCashCouponRepositoryCustom {

    private Logger log = LoggerFactory.getLogger(MallCashCouponRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     *
     * @param mallId
     * @param faceValue
     * @param startTime
     * @param endTime
     * @param channelId
     * @param status
     * @param pageResult
     * @return
     */
    @Override
    public PageResult<MallCashCouponDto> findIntegrationCashCouponPage(Long mallId, BigDecimal faceValue, String startTime, String endTime, Long channelId, String status, PageResult pageResult) {


        SQLPageDoing doing = (sql, fields, params, typeReferenceMap) -> {
//            sql.append("SELECT %s FROM intergration_cash_wallet f ");
            sql.append("SELECT %s FROM mall_cash_coupon f Left join integration_channel e on f.`integration_channel_id` = e.id  WHERE   f.is_delete= 'NO' and f.mall_id = :mallId ");
            params.put("mallId", mallId);
            if ("ISSUED".equals(status)) {
                sql.append(" AND f.status in ('ISSUED','SOLDOUT')");
            } else {
                sql.append("and status = :status");
                params.put("status", status);
            }


            if (StringUtils.isNotBlank(startTime)) {
                sql.append(" AND f.date_created>=:startTime");
                params.put("startTime", startTime);
            }

            if (StringUtils.isNotBlank(endTime)) {
                sql.append(" AND f.date_created<=:endTime");
                params.put("endTime", endTime);
            }

            if (faceValue != null) {
                sql.append(" AND f.face_value = :faceValue");
                params.put("faceValue", faceValue);
            }

            if (channelId != null) {
                sql.append(" AND f.integration_channel_id = :channelId");
                params.put("channelId", channelId);
            }

            sql.append(" order by case when f.status='SOLDOUT' THEN 1 ELSE 0 END , f.date_created desc");
            fields.append(" f.name,f.pic_url,f.batch_put_no,f.id,f.date_created,f.face_value,f.quantity,f.mall_id,f.`status`,e.`name` as integrationChannelName,e.id as integrationChannelId,case when status='SOLDOUT' THEN 1 ELSE 0 END AS can_stop,  case when integration_channel_id  is not null or status='SOLDOUT' THEN 1 ELSE 0 END AS can_put ");
//            log.info("======================>my sql: "+sql.toString());
        };
        return doing.go(pageResult, MallCashCouponDto.class, objectMapper, jdbcTemplate, log);
    }

    /**
     *
     * @param integrationChannelId
     * @return
     */
    @Override
    public Long queryTotalNum(Long integrationChannelId) {
        SQLObjectDoing<Long> result = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall_cash_coupon where integration_channel_id= :integrationChannelId");
            params.put("integrationChannelId", integrationChannelId);
            fields.append(" sum(quantity) ");
        };
        return result.go(Long.class, jdbcTemplate, log, objectMapper);
    }

    /**
     *
     * @param mallId
     * @param integrationChannelId
     * @return
     */
    @Override
    public Long queryMaxBatchPutNo(Long mallId, Long integrationChannelId) {
        SQLObjectDoing<Long> result = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall_cash_coupon where integration_channel_id= :integrationChannelId and mall_id =:mallId for update");
            params.put("integrationChannelId", integrationChannelId);
            params.put("mallId", mallId);
            fields.append(" case when max(batch_put_no) is null then 0 else max(batch_put_no) end");
        };
        return result.go(Long.class, jdbcTemplate, log, objectMapper);
    }

    /**
     *
     * @param integrationChannelId
     * @return
     */
    @Override
    public List<IntegrationCouponRespDto> queryAllCashCoupon(Long integrationChannelId) {
        SQLListDoing<IntegrationCouponRespDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append(" select %s from  mall_cash_coupon ");
            sql.append(" where integration_channel_id =:integrationChannelId ");
            sql.append(" and status='ISSUED' and is_delete='NO' ");
            params.put("integrationChannelId", integrationChannelId);
            fields.append(" id,face_value");
        };
        return doing.go(IntegrationCouponRespDto.class, objectMapper, jdbcTemplate, log);
    }
}
